// import { delSalesMen } from '../../../insurance-server/dao/salesMenDao';
import apis from '../../http/apis'
export default {
    namespaced: true,
    state: {
        salesMen: [],
        pages: 1,
        currentPage: 1,
        pageSize: 0,
        total: 5,
        images: "",
    },
    mutations: {
        setPageData (state, pageData) {
            state.pages = pageData.pages;
            state.total = pageData.total;
            state.salesMen = pageData.salesMen;
        },
        OneSalesMen (state, saleMan) {
            state.salesMen = saleMan.data[0]
        }
    },
    actions: {
        // 获取销售员
        async getSalesMen (context) {
            const { currentPage, pageSize } = context.state
            const { data } = await apis.salesMen.getSalesMen({
                currentPage: 1,
                pageSize: 5
            })
            // console.log(data);
            context.commit('setPageData', data.data)
            this.salesMen = data.data
            // 判断销售员有无上传照片 若无则给默认头像
            this.salesMen.salesMen.forEach(item => {
                if (item.images === "") {
                    item.images = 'photo.png'
                }
            })
        },
        // 添加销售员
        async addSalesMen (context, salesMen) {
            const { data } = await apis.salesMen.addSalesMen(salesMen)
            if (data.status) {
                alert('添加成功');
                await context.dispatch('getSalesMen')
            }
        },
        // 删除销售员
        async delSalesMen (context, _id) {
            const { data } = await apis.salesMen.delSalesMen({ _id })
            if (data.status) {
                alert('删除成功');
                await context.dispatch('getSalesMen')
            }
        },
        //获取单个销售老哥数据
        async getSalesMenById (context, _id) {
            const { data } = await apis.salesMen.getSalesMenById(_id)
            // context.commit('OneSalesMen', data)
            return data

        },

        //修改销售员数据
        async updateSalesMen (context, newSalesMenData) {
            // console.log(123, newSalesMenData);
            const { data } = await apis.salesMen.updateSalesMen({
                ...newSalesMenData
            })
            if (data.status) {
                alert('修改成功')
            }
            console.log(data)
        }
    }
}